package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.HomePageCustomTypeEnum;
import com.xbongbong.paas.enums.OptionalRangeEnum;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.log.entity.OperateLogEntity;
import com.xbongbong.paas.log.model.OperateLogModel;
import com.xbongbong.paas.manager.feign.ChartCategoryFeignClient;
import com.xbongbong.paas.service.HomePageManageService;
import com.xbongbong.paas.service.PackageService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.domain.entity.HomePageManageEntity;
import com.xbongbong.pro.domain.entity.IndexSetEntity;
import com.xbongbong.pro.enums.BoardTypeEnum;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.pro.enums.errorcodes.IndexErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.ControlCenterMenuEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.homePageManage.pojo.HomePageManageListPojo;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageCopyDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageDelDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageEnableDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageGetDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageListDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageManageSaveDTO;
import com.xbongbong.pro.homePageManage.pojo.dto.HomePageVisibleDTO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageCopyVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageDelVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageEnableVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageGetVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageListVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageManageSaveVO;
import com.xbongbong.pro.homePageManage.pojo.vo.HomePageVisibleVO;
import com.xbongbong.pro.log.pojo.DetailLogPojo;
import com.xbongbong.pro.statistic.constant.ChartConstant;
import com.xbongbong.pro.statistic.pojo.dto.CategoryPublishDTO;
import com.xbongbong.pro.statistic.pojo.vo.CategorySearchVO;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryInitVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.model.HomePageManageModel;
import com.xbongbong.saas.model.IndexSetModel;
import com.xbongbong.saas.service.toolbox.help.LogHelp;
import com.xbongbong.sys.model.CompanyConfigModel;
import com.xbongbong.sys.model.UserConfigModel;
import com.xbongbong.workflow.pojo.User;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName HomePageManageServiceImpl
 * @Description 首页管理
 * @Author mengna.Deng
 * @Date 2021/11/1 14:13
 * @Version 1.0
 */
@Service("homePageManageService")
public class HomePageManageServiceImpl implements HomePageManageService {

    private static final Logger LOG = LoggerFactory.getLogger(HomePageManageServiceImpl.class);

    @Resource
    private HomePageManageModel homePageManageModel;
    @Resource
    private OperateLogModel operateLogModel;
    @Resource
    private PackageService packageService;
    @Resource
    private LogHelp logHelp;
    @Resource
    private MongoLogHelp mongoLogHelp;
    @Resource
    private IndexSetModel indexSetModel;
    @Resource
    private ChartCategoryFeignClient chartCategoryFeignClient;

    @Override
    public HomePageManageListVO list(HomePageManageListDTO listDTO) throws XbbException {
        HomePageManageListVO listVO = new HomePageManageListVO();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, listDTO.getCorpid());
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("orderByStr"," update_time DESC");
        PageHelper pageHelper = PageHelperUtil.getPageHelper(param, homePageManageModel, listDTO.getPage(),listDTO.getPageSize());
        List<HomePageManageEntity> list = (List<HomePageManageEntity>) PageHelperUtil.getEntityList(param, pageHelper, homePageManageModel);
        List<HomePageManageListPojo> pojoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)){
            for (HomePageManageEntity entity : list) {
                HomePageManageListPojo pojo = new HomePageManageListPojo();
                pojo.setId(entity.getId());
                pojo.setCorpid(entity.getCorpid());
                pojo.setDel(entity.getDel());
                pojo.setDescription(entity.getDescription());
                pojo.setEnable(entity.getEnable());
                pojo.setName(entity.getName());
                pojo.setUpdateTime(entity.getUpdateTime());
                JSONArray range = entity.getRange();
                pojo.setRange(range);
                // 将range拼接成rangeStr
                StringBuffer sb = new StringBuffer();
                if (CollectionUtils.isNotEmpty(range)) {
                    for (Object item : range) {
                        JSONObject jsonObject = (JSONObject) item;
                        String name = jsonObject.getString("name");
                        if (StringUtils.isBlank(name)){
                            continue;
                        }
                        if (StringUtils.isNotBlank(sb.toString())) {
                            sb.append(StringConstant.CHINESE_COMMA);
                        }
                        sb.append(name);
                    }
                }
                pojo.setRangeStr(sb.toString());
                pojoList.add(pojo);
            }
        }
        listVO.setList(pojoList);
        listVO.setPageHelper(pageHelper);
        return listVO;
    }

    @Override
    public HomePageManageGetVO get(HomePageManageGetDTO getDTO) throws XbbException {
        HomePageManageGetVO getVO = new HomePageManageGetVO();

        HomePageManageEntity entity = homePageManageModel.getByKey(getDTO.getId(),getDTO.getCorpid());
        if (null != entity){
            getVO.setCorpid(entity.getCorpid());
            getVO.setAddTime(entity.getAddTime());
            getVO.setDel(entity.getDel());
            getVO.setDescription(entity.getDescription());
            getVO.setEnable(entity.getEnable());
            getVO.setId(entity.getId());
            getVO.setName(entity.getName());
            getVO.setRange(entity.getRange());
            getVO.setUpdateTime(entity.getUpdateTime());
            getVO.setRefId(entity.getRefIds());
        }

        return getVO;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public HomePageManageSaveVO save(HomePageManageSaveDTO saveDTO) throws XbbException {
        HomePageManageSaveVO saveVO = new HomePageManageSaveVO();

        // 参数校验
        String corpid = saveDTO.getCorpid();

        boolean isAdd = false;
        HomePageManageEntity oldEntity = new HomePageManageEntity();
        HomePageManageEntity entity = new HomePageManageEntity();
        Long id = saveDTO.getId();
        if (id == null || id == 0) {
            isAdd = true;
            // 套餐的判断
            checkPackageLimit(corpid);
            // 默认不启用
            entity.setEnable(BasicConstant.NO_USE);
            BeanUtil.copyProperties(saveDTO, entity);
        } else {
//            checkOverLimit(corpid);
            oldEntity = homePageManageModel.getByKey(id, corpid);
            if (oldEntity == null) {
                throw new XbbException(IndexErrorCodeEnum.API_ERROR_290006, IndexErrorCodeEnum.API_ERROR_290006.getMsg());
            }
            BeanUtil.copyProperties(saveDTO, entity);
            entity.setUpdateTime((long) DateUtil.getInt());
        }
        // 使用范围
        JSONArray range = new JSONArray();
        range.addAll(saveDTO.getRange());
        entity.setRange(range);
        try {
            homePageManageModel.save(entity);
        } catch (Exception e) {
            LOG.error("首页保存失败", e);
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290006, IndexErrorCodeEnum.API_ERROR_290006.getMsg());
        }

        // 日志
        String loginUserName = saveDTO.getLoginUserName();
        OperateTypeEnum operateTypeEnum = isAdd ? OperateTypeEnum.NEW : OperateTypeEnum.EDIT;
        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_HOME_PAGE_MANAGE), loginUserName + operateTypeEnum.getName(), entity.getName());
        if (isAdd) {
            OperateLogEntity logEntity = new OperateLogEntity(corpid, saveDTO.getUserId(), loginUserName, OperateModuleTypeEnum.HOME_PAGE_MANAGE,
                    operateTypeEnum, entity.getId().toString(), ControlCenterMenuEnum.CUSTOM_PAGE.getName(), memo, saveDTO.getHttpHeader(), null);
            operateLogModel.insert(logEntity);
        } else {
            JSONObject oldObj = JsonHelperUtil.parseObject(JSONObject.toJSONString(oldEntity));
            JSONObject newObj = JsonHelperUtil.parseObject(JSONObject.toJSONString(entity));
            List<DetailLogPojo> detailArr = logHelp.deatilLog4HomePageManage(corpid, oldObj, newObj);
            mongoLogHelp.buildLog4DetailArr(detailArr, corpid, saveDTO.getUserId(), loginUserName, OperateModuleTypeEnum.HOME_PAGE_MANAGE, OperateTypeEnum.EDIT, id.toString(), ProPermissionAliasEnum.HOME_PAGE_MANAGE.getMemo(), memo, saveDTO.getHttpHeader());
        }

        saveVO.setId((entity.getId()).intValue());
        return saveVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HomePageManageCopyVO copy(HomePageManageCopyDTO copyDTO) throws XbbException {
        HomePageManageCopyVO copyVO = new HomePageManageCopyVO();
        String corpid = copyDTO.getCorpid();
        // 数量限制
        checkOverLimit(corpid);
        HomePageManageEntity entity = homePageManageModel.getByKey(copyDTO.getId(), corpid);
        if (entity == null) {
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290004,  IndexErrorCodeEnum.API_ERROR_290004.getMsg());
        }
        HomePageManageEntity copyEntity = new HomePageManageEntity();
        copyEntity.setCorpid(corpid);
        copyEntity.setRange(entity.getRange());
        copyEntity.setDescription(entity.getDescription());
        copyEntity.setEnable(BasicConstant.ZERO);
        copyEntity.setDel(DelEnum.NORMAL.getDel());
        String copyName = entity.getName() + StringConstant.HOME_PAGE_COPY_ADD;
        if (copyName.length() > StringConstant.HOME_PAGE_NAME_LENGTH){
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290003, IndexErrorCodeEnum.API_ERROR_290003.getMsg());
        }
        copyEntity.setName(copyName);
        // pro_index_set表数据复制
        JSONObject refIds = entity.getRefIds();
        JSONObject copyRefIds = new JSONObject();
        List<IndexSetEntity> insertList = new ArrayList<>();
        if (null != refIds){
            List<String> boardAliasList = BoardTypeEnum.getBoardAlias();
            long now = DateUtil.getTodayInt();
            for (String boardAlias : boardAliasList) {
                Long refId = refIds.getLong(boardAlias);
                if (null != refId) {
                    IndexSetEntity indexSetEntity = indexSetModel.getByKey(refId, corpid);
                    if (null == indexSetEntity){
                        throw new XbbException(IndexErrorCodeEnum.API_ERROR_290005,  IndexErrorCodeEnum.API_ERROR_290005.getMsg());
                    }
                    indexSetEntity.setId(null);
                    indexSetEntity.setAddTime(now);
                    indexSetEntity.setUpdateTime(now);
                    insertList.add(indexSetEntity);
                    copyRefIds.put(boardAlias, indexSetEntity.getId());
                }
            }
        }
        try {
            // 批量插入看板
            indexSetModel.insertBatch(insertList);
            insertList.forEach(item ->{
                BoardTypeEnum boardTypeEnum = BoardTypeEnum.getByCode(item.getBoardType());
                switch (boardTypeEnum){
                    case WEB_LEFT:
                    case WEB_RIGHT:
                    case DINGTALK:
                    case FOCUS_DINGTALK_CATEGORY:
                    case FOCUS_DINGTALK_WORK:
                        copyRefIds.put(boardTypeEnum.getAlias(),item.getId());
                        break;
                    default:
                        break;
                }
            });
            // 复制的首页管理插入
            copyEntity.setRefIds(copyRefIds);
            homePageManageModel.insert(copyEntity);
        } catch (Exception e) {
            LOG.error("首页保存失败", e);
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290006, IndexErrorCodeEnum.API_ERROR_290006.getMsg());
        }
        // 日志
        String loginUserName = copyDTO.getLoginUserName();
        OperateTypeEnum operateTypeEnum = OperateTypeEnum.COPY;
        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_HOME_PAGE_MANAGE), loginUserName + operateTypeEnum.getName(), entity.getName());
        OperateLogEntity logEntity = new OperateLogEntity(corpid, copyDTO.getUserId(), loginUserName, OperateModuleTypeEnum.HOME_PAGE_MANAGE,
                operateTypeEnum, entity.getId().toString(), ControlCenterMenuEnum.CUSTOM_PAGE.getName(), memo, copyDTO.getHttpHeader(), null);
        operateLogModel.insert(logEntity);

        copyVO.setId(copyEntity.getId());
        return copyVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HomePageManageEnableVO enable(HomePageManageEnableDTO enableDTO) throws XbbException {
        HomePageManageEnableVO enableVO = new HomePageManageEnableVO();

        String corpid = enableDTO.getCorpid();
        Integer enable = enableDTO.getEnable();
        boolean isEnable = BasicConstant.IS_USE.equals(enable);
        HomePageManageEntity entity = homePageManageModel.getByKey(enableDTO.getId(), corpid);
        if (entity == null) {
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290007,  IndexErrorCodeEnum.API_ERROR_290007.getMsg());
        }
        String enableStr = isEnable ? "启用" : "关闭";
        if (entity.getEnable().equals(enable)) {
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290002, IndexErrorCodeEnum.API_ERROR_290002.getMsg());
        }

        // 发布/撤销到首页
        publish(enableDTO, entity.getName());

        entity.setEnable(enableDTO.getEnable());
        entity.setUpdateTime((long) DateUtil.getInt());
        try {
            homePageManageModel.update(entity);
        } catch (Exception e) {
            LOG.error("首页" + enableStr + "失败", e);
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290007, IndexErrorCodeEnum.API_ERROR_290007.getMsg());
        }
        // 日志
        String loginUserName = enableDTO.getLoginUserName();
        OperateTypeEnum operateTypeEnum = enable.equals(BasicConstant.NO_USE) ? OperateTypeEnum.CLOSE : OperateTypeEnum.ENABLE;
        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_HOME_PAGE_MANAGE_ENABLE), loginUserName + enableStr, entity.getName());
        OperateLogEntity logEntity = new OperateLogEntity(corpid, enableDTO.getUserId(), loginUserName, OperateModuleTypeEnum.HOME_PAGE_MANAGE,
                operateTypeEnum, entity.getId().toString(), ControlCenterMenuEnum.CUSTOM_PAGE.getName(), memo, enableDTO.getHttpHeader(), null);
        operateLogModel.insert(logEntity);

        return enableVO;
    }
    
    @Override
    public HomePageManageDelVO delete(HomePageManageDelDTO delDTO) throws XbbException {
        HomePageManageDelVO delVO = new HomePageManageDelVO();

        Long id = delDTO.getId();
        String corpid = delDTO.getCorpid();

        HomePageManageEntity entity = homePageManageModel.getByKey(id, corpid);
        if (entity == null) {
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290008, IndexErrorCodeEnum.API_ERROR_290008.getMsg());
        }

        try {
            homePageManageModel.deleteByKey(id, corpid);
        } catch (Exception e) {
            LOG.error("首页删除失败", e);
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290008, IndexErrorCodeEnum.API_ERROR_290008.getMsg());
        }

        // 日志
        String loginUserName = delDTO.getLoginUserName();
        String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_DELETE_WORK), loginUserName , OperateModuleTypeEnum.HOME_PAGE_MANAGE.getName(), entity.getName());
        OperateLogEntity logEntity = new OperateLogEntity(corpid, delDTO.getUserId(), loginUserName, OperateModuleTypeEnum.HOME_PAGE_MANAGE,
                OperateTypeEnum.DELETE, entity.getId().toString(), ControlCenterMenuEnum.CUSTOM_PAGE.getName(), memo, delDTO.getHttpHeader(), null);
        operateLogModel.insert(logEntity);
        delVO.setId(id.intValue());
        return delVO;
    }

    /**
     * 检验自定义首页数量是否超过限制
     * @param corpid
     * @throws XbbException
     */
    private void checkPackageLimit(String corpid) throws XbbException {
        // 获取自定义首页数量总数
        JSONObject jsonObject = packageService.getValueAddInfo(corpid, ValueAddedTypeEnum.HOME_PAGE, null, null);
        Integer totalCount = jsonObject.getInteger("totalValue");
        Integer usedValue = jsonObject.getInteger("usedValue");
        Integer num = usedValue - totalCount;
        if (num>=0) {
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290001,String.format(IndexErrorCodeEnum.API_ERROR_290001.getMsg(), PaasConstant.HOME_PAGE_SIZE));
        }
    }

    private void checkOverLimit(String corpid) throws XbbException {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("del",DelEnum.NORMAL);
        Integer count = homePageManageModel.getEntitysCount(param);
        if (count >= PaasConstant.HOME_PAGE_SIZE){
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290001,String.format(IndexErrorCodeEnum.API_ERROR_290001.getMsg(), PaasConstant.HOME_PAGE_SIZE));
        }
    }
    @Override
    public HomePageVisibleVO homePageListIsVisible(HomePageVisibleDTO homePageVisibleDTO) {
        HomePageVisibleVO homePageVisibleVO = new HomePageVisibleVO();
        UserVO userVO = homePageVisibleDTO.getLoginUser();
        List<HomePageManageEntity> list = homePageVisibleDTO.getList();
        if (CollectionUtils.isEmpty(list)){
            homePageVisibleVO.setList(new ArrayList<>());
            return homePageVisibleVO;
        }
        List<Long> departmentIdList = userVO.getDepSet().stream().map(u->u.getId()).collect(Collectors.toList());
        List<Integer> roleIdList = userVO.getRoleSet().stream().map(u->u.getId()).collect(Collectors.toList());
        String userId = userVO.getUserId();
        list.removeIf(entity->{
            Boolean flag = homePageIsVisible(entity,departmentIdList,roleIdList,userId);
            return !flag;
        });
        homePageVisibleVO.setList(list);
        return homePageVisibleVO;
    }

    @Override
    public Boolean judgeDistributeByUserId(BaseDTO baseDTO) {
        HashMap<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",baseDTO.getCorpid());
        param.put("del",DelEnum.NORMAL);
        param.put("enable",1);
        List<HomePageManageEntity> list = homePageManageModel.findEntitys(param);
        UserVO userVO = baseDTO.getLoginUser();
        List<Long> departmentIdList = userVO.getDepSet().stream().map(u->u.getId()).collect(Collectors.toList());
        List<Integer> roleIdList = userVO.getRoleSet().stream().map(u->u.getId()).collect(Collectors.toList());
        String userId = userVO.getUserId();
        for (HomePageManageEntity entity : list) {
            if (homePageIsVisible(entity, departmentIdList,roleIdList,userId)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断自定义首页可见性
     * @param homePageManageEntity
     * @return
     */
    public Boolean homePageIsVisible(HomePageManageEntity homePageManageEntity,List<Long> departmentIdList,List<Integer> roleIdList,String userId) {
        if (null == homePageManageEntity){
            return false;
        }
        JSONArray range = homePageManageEntity.getRange();
        if (null == range){
            return false;
        }
        List<OptionalRangeEntity> optionalRangeList = range.toJavaList(OptionalRangeEntity.class);
        for (OptionalRangeEntity optionalRangeEntity : optionalRangeList) {
            String id = optionalRangeEntity.getId();
            String property = optionalRangeEntity.getProperty();
            OptionalRangeEnum rangeEnum = OptionalRangeEnum.getByValue(property);
            switch (rangeEnum) {
                case DEPT:
                    if (departmentIdList.contains(Long.parseLong(id))) {
                        return true;
                    }
                    break;
                case ROLE:
                    if (roleIdList.contains(Integer.parseInt(id))) {
                        return true;
                    }
                    break;
                case USER:
                    if (Objects.equals(userId, id)) {
                        return true;
                    }
                    break;
            }
        }
        return false;
    }

    private void publish(HomePageManageEnableDTO enableDTO,String name) throws XbbException {
        CategoryPublishDTO categoryPublishDTO = new CategoryPublishDTO();
        categoryPublishDTO.setPlatform(enableDTO.getPlatform());
        categoryPublishDTO.setCategoryId(enableDTO.getId());
        categoryPublishDTO.setCategoryName(name);
        categoryPublishDTO.setPublish(enableDTO.getEnable());
        categoryPublishDTO.setIsCustom(HomePageCustomTypeEnum.IS_CUSTOM.getCode());
        categoryPublishDTO.setCorpid(enableDTO.getCorpid());
        categoryPublishDTO.setUserId(enableDTO.getUserId());
        categoryPublishDTO.setHead(ChartConstant.PUBLISH_HEAD);
        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.toString();
        XbbResponse<CategorySearchVO> initVOXbbResp = chartCategoryFeignClient.categoryPublish(categoryPublishDTO, lang);

        if (!initVOXbbResp.isSuccess()) {
            // 只有开启或者关闭会触发
            throw new XbbException(IndexErrorCodeEnum.API_ERROR_290007, initVOXbbResp.getMsg());
        }
    }
}
